package com.miao.pay;

import com.alipay.api.AlipayApiException;
import com.alipay.api.AlipayClient;
import com.alipay.api.request.AlipayTradePagePayRequest;
import com.alipay.api.response.AlipayTradePagePayResponse;
import com.miao.config.AlipayConfig;
import jakarta.annotation.Resource;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

@Slf4j
@Component
public class AliPay implements MiaoPayType{

    @Resource
    private AlipayClient alipayClient;

    @Resource
    private AlipayTradePagePayRequest alipayTradePagePayRequest;

    @Override
    public String Pay(PayDTO dto) throws AlipayApiException {
        //1.获得初始化的AlipayClient
        //2.设置请求参数AlipayTradePagePayRequest
        alipayTradePagePayRequest.setReturnUrl(AlipayConfig.return_url);
        alipayTradePagePayRequest.setNotifyUrl(AlipayConfig.notify_url);

        alipayTradePagePayRequest.setBizContent(
                "{\"out_trade_no\":\""+ dto.getTradeNo() +"\","
                        + "\"total_amount\":\""+ dto.getTotalAmount() +"\","
                        + "\"subject\":\""+ dto.getSubject() +"\","
                        + "\"body\":\""+ dto.getBody() +"\","
                        + "\"product_code\":\"FAST_INSTANT_TRADE_PAY\"}");
        //请求
        AlipayTradePagePayResponse response = alipayClient.pageExecute(alipayTradePagePayRequest);
        log.info(response.getBody());
        return alipayClient.pageExecute(alipayTradePagePayRequest).getBody();
    }
}
